package ProjectManagement;

import com.spm.pojo.StudentGrade;
import com.spm.pojo.Teacher;
import com.spm.service.GradeService;
import com.spm.service.impl.GradeServiceImpl;
import com.spm.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ShowOneGradeServlet" ,value="/ShowOneGradeServlet")
public class ShowOneGradeServlet extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GradeService gradeService = new GradeServiceImpl(sqlSession);
        //1、获取session中保存的教师类
        HttpSession session =  request.getSession();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        //从教师类中获取教师编号
        String tid = teacher.getTid();
        //2、从request中获取cno参数和sid参数
        String cno = request.getParameter("cno");
        String sid = request.getParameter("searchId");

        //3、调用gradeService获取成绩
        StudentGrade studentGrade = gradeService.queryByCAS(tid, cno, sid);
        sqlSession.close();
        //4、保存studentGrades至request域中
        request.setAttribute("gradeOfOne", studentGrade);
        String mainPage = "OneGradeDisPlays.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }

}